package javase2.day03.demo02;
/*
HashSet储存自定义类型的元素：
    set集合储存的元素必须重写hashCode和equals方法
    要求：同名同龄视为同一个人，只能储存一次
 */
import java.util.HashSet;

public class Demo03HashSetStudent {
    public static void main(String[] args) {
        HashSet<Student> stuSet=new HashSet<>();
        Student stu1=new Student("孙悟空",19);
        Student stu2=new Student("孙行者",19);
        Student stu3=new Student("孙悟空",19);
        stuSet.add(stu1);
        stuSet.add(stu2);
        stuSet.add(stu3);
        System.out.println(stuSet);//[Student{name='孙行者', age=19}, Student{name='孙悟空', age=19}, Student{name='孙悟空', age=19}]
        System.out.println(stu1.hashCode());//1163157884
        System.out.println(stu3.hashCode());//356573597
        System.out.println(stu1==stu3);//false
        System.out.println(stu1.equals(stu3));//false

        //重写hashCode和equals方法后
        System.out.println(stuSet);//[Student{name='孙行者', age=19}, Student{name='孙悟空', age=19}]
        System.out.println(stu1.hashCode());//721405824
        System.out.println(stu3.hashCode());//721405824
        System.out.println(stu1==stu3);//false
        System.out.println(stu1.equals(stu3));//true

    }

}
